package com.study.zgw.nio;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;

public class Hello {

    public static void main(String[] args) {

        try {
            FileChannel channel = new FileInputStream("src/data.txt").getChannel();
            ByteBuffer byteBuffer = ByteBuffer.allocate(10);
            while (true){
                int readNum = channel.read(byteBuffer);
                System.out.println("读取的字节数量-----》"+readNum);
                if (-1 == readNum){
                    break;
                }
                byteBuffer.flip();//切换到读模式
                while (byteBuffer.hasRemaining()){
                    System.out.println("位置:--->"+byteBuffer.position());
                    byte b = byteBuffer.get();
                    System.out.println("读取的字节:--->"+(char)b);
                }
                byteBuffer.clear();
            }


        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
